<script>
import { Search, ArrowRight, ArrowLeft } from "@element-plus/icons-vue";
import { ImageAPI } from "../apis";

export default {
  data() {
    return {
      query: {
        page: 1,
        page_size: 40,
        keywords: "",
      },
      users: [],
      has_more: false,
      editing_user: {
        user_id: null,
        name: null,
        phone: null,
        address: null,
      },
    };
  },
  components: {
    Search,
    ArrowRight,
    ArrowLeft,
  },

  async mounted() {
    await this.getUsers();
  },
  methods: {
    async getUsers() {
      const users = await ImageAPI.getUsers(this.query);
      this.users = users;
      this.has_more = users.length === this.query.page_size;
    },

    async nextPage() {
      this.query.page += 1;
      await this.getUsers();
    },
    async prevPage() {
      this.query.page -= 1;
      await this.getUsers();
    },

    async confirmKeywords(page) {
      this.query.page = isNaN(page) ? 1 : page;
      await this.getUsers();
    },

    // 点击顾客行
    async clickUserRow(user) {
      const index = this.users.findIndex((c) => c.user_id === user.user_id);
      if (index < 0) return;
      this.editing_user = Object.assign({}, user);
    },

    // 增改顾客
    async upsertUser() {
      // 更新
      if (this.editing_user.user_id) {
        const user = await ImageAPI.updateUser(this.editing_user);
        const index = this.users.findIndex((c) => c.user_id === user.user_id);
        if (index < 0) return;
        this.users[index] = user;
        this.reset();
      } else {
        const user = await ImageAPI.createUser(this.editing_user);
        this.users.splice(0, 0, user);
        this.reset();
      }
    },

    reset() {
      this.editing_user = {
        user_id: null,
        name: null,
        phone: null,
        address: null,
      };
    },
  },
};
</script>

<template>
  <el-container style="calc(100vh - 100px); overflow: auto;">
    <!-- 表格 -->
    <el-main style="padding: 5px">
      <div class="filter-header">
        <!-- 底部换页 -->
        <div style="display: flex">
          <el-input
            v-model="query.keywords"
            clearable
            placeholder="搜索关键词"
            size="large"
            @keyup.enter="confirmKeywords"
          >
            <template #prefix>
              <el-icon class="el-input__icon"><search /></el-icon>
            </template>
          </el-input>
          <div style="flex: 1; margin-left: 20px">
            <el-button
              @click="prevPage"
              :disabled="query.page === 1"
              size="large"
              style="width: 100%"
            >
              <el-icon size="20"><arrow-left /></el-icon>
            </el-button>
          </div>
          <div style="flex: 1; margin-left: 5px">
            <el-button
              @click="nextPage"
              :disabled="!has_more"
              size="large"
              style="width: 100%"
            >
              <el-icon size="20"><arrow-right /></el-icon>
            </el-button>
          </div>
        </div>
        <div style="margin-left: 20px">
          <el-button type="primary" @click="reset" size="large"
            >添加客户</el-button
          >
        </div>
      </div>
      <el-table
        :data="users"
        highlight-current-row
        height="88vh"
        @row-click="clickUserRow"
        stripe
      >
        <el-table-column prop="name" label="名称" width="200" />
        <el-table-column prop="phone" label="手机" width="160" />

        <el-table-column prop="address" label="地址" width="auto" />
      </el-table>
    </el-main>
    <el-aside width="400px" style="padding: 5px">
      <el-card shadow="hover">
        <template #header>
          <div class="flex-between">
            <div style="font-size: var(--el-font-size-large)">增改客户</div>
            <div>
              <el-button type="primary" @click="upsertUser" size="large">
                保存
              </el-button>
            </div>
          </div>
        </template>
        <el-form :model="editing_user" label-width="60px">
          <el-form-item label="名称" prop="name">
            <el-input v-model="editing_user.name" />
          </el-form-item>
          <el-form-item label="手机" prop="phone">
            <el-input v-model="editing_user.phone" />
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="editing_user.address" />
          </el-form-item>
        </el-form>
      </el-card>
    </el-aside>
  </el-container>
</template>
